﻿using iBuproModel.Providers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web.Security;
using iBuproModel.Classes;
using iBuproModel;
using System.Linq;

namespace IBupro.Tests
{
    
    
    /// <summary>
    ///This is a test class for IBuproMembershipProviderTest and is intended
    ///to contain all IBuproMembershipProviderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IBuproMembershipProviderTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        

        /// <summary>
        ///A test for ChangePassword
        ///</summary>
        [TestMethod()]
        public void ChangePasswordTest()
        {
            try
            {
                Entities context = new Entities();

                IBuproMembershipProvider provider = new IBuproMembershipProvider();

                MembershipCreateStatus status;
                Random rand = new Random();

                string username = rand.Next(10000000).ToString();
                string oldPassword = "aaa";
                string newPassword = "bbb";

                User user = context.Users.Where(u => u.Username == username).SingleOrDefault();
                if (user != null)
                {
                    context.Users.DeleteObject(user);
                }

                context.SaveChanges();
                provider.CreateUser(username, oldPassword, username+"@swdsa.cz", "aaa", "bbb", out status);
                context.SaveChanges();
                provider.ChangePassword(username, oldPassword, newPassword);

                user = context.Users.Where(u => u.Username == username).SingleOrDefault();
                string newHash = Cryptography.GetSHA256(provider.CombineSaltAndPassword(user.Salt, user.Password));

                user.Password = newHash;

                context.SaveChanges();

                Assert.AreEqual(newHash, user.Password);
            }
            catch
            {
                Assert.Fail("Problem with changing password");
            }
            
        }

        /// <summary>
        ///A test for ChangePasswordQuestionAndAnswer
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException), "Not implemented")]
        public void ChangePasswordQuestionAndAnswerTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            mp.ChangePasswordQuestionAndAnswer(null,null,null,null);
        }

        /// <summary>
        ///A test for CombineSaltAndPassword
        ///</summary>
        [TestMethod()]
        [DeploymentItem("iBuproModel.dll")]
        public void CombineSaltAndPasswordTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            string salt = "aaa";
            string password = "bbb";
            Assert.AreEqual(salt + password, mp.CombineSaltAndPassword(salt,password));
        }

        /// <summary>
        ///A test for CreateUser
        ///</summary>
        [TestMethod()]
        public void CreateUserTest()
        {
            string userName="qwerew";
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            if (mp.UserExists(userName))
                mp.DeleteUser(userName, false);

            MembershipCreateStatus stat;
            mp.CreateUser(userName, "aaa", "dasd@dsad.cz", "aaa", "bbb",out stat);
            
            Assert.IsTrue(mp.UserExists(userName));

            mp.DeleteUser(userName, false);
        }

       

        /// <summary>
        ///A test for DeleteUser
        ///</summary>
        [TestMethod()]
        public void DeleteUserTest()
        {
            string userName = "qwerew";
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            
            if (!mp.UserExists(userName))
            {
                MembershipCreateStatus stat;
                mp.CreateUser(userName, "aaa", "dasd@dsad.cz", "aaa", "bbb", out stat);
            }

            mp.DeleteUser(userName, false);
            Assert.IsFalse(mp.UserExists(userName));

        }

        /// <summary>
        ///A test for FindUsersByEmail
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void FindUsersByEmailTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            string emailToMatch = string.Empty; 
            int pageIndex = 0; 
            int pageSize = 0; 
            int totalRecords = 0; 
            int totalRecordsExpected = 0; 
            MembershipUserCollection expected = null; 
            MembershipUserCollection actual;
            actual = target.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
            Assert.AreEqual(totalRecordsExpected, totalRecords);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for FindUsersByName
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void FindUsersByNameTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            int p;
            mp.FindUsersByName(null,0,0, out p);
        }

        /// <summary>
        ///A test for GetAllUsers
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void GetAllUsersTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            int p;
            mp.GetAllUsers(0, 0,out p);
        }

        /// <summary>
        ///A test for GetNumberOfUsersOnline
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void GetNumberOfUsersOnlineTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            mp.GetNumberOfUsersOnline();
        }

        /// <summary>
        ///A test for GetPassword
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void GetPasswordTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            mp.GetPassword(null, null);
           
        }

        /// <summary>
        ///A test for GetUser
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void GetUserTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            mp.GetPassword(null, null);
        }

        /// <summary>
        ///A test for GetUserNameByEmail
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void GetUserNameByEmailTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            mp.GetUserNameByEmail(null);
        }

        /// <summary>
        ///A test for ResetPassword
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void ResetPasswordTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            string username = string.Empty; 
            string answer = string.Empty; 
            string expected = string.Empty; 
            string actual;
            actual = target.ResetPassword(username, answer);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for UnlockUser
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void UnlockUserTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            string userName = string.Empty; 
            bool expected = false; 
            bool actual;
            actual = target.UnlockUser(userName);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for UpdateUser
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void UpdateUserTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            MembershipUser user = null; 
            target.UpdateUser(user);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for ValidateUser
        ///</summary>
        [TestMethod()]
        public void ValidateUserTest()
        {
            string userName = "qwerew";
            string password = "passwqewqeweq";
            IBuproMembershipProvider mp = new IBuproMembershipProvider();

            if (!mp.UserExists(userName))
            {
                MembershipCreateStatus stat;
                mp.CreateUser(userName, password, "dasd@dsad.cz", "aaa", "bbb", out stat);
            }

            Assert.IsTrue(mp.ValidateUser(userName,password));
            mp.DeleteUser(userName,false);
                        
        }

        /// <summary>
        ///A test for ApplicationName
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void ApplicationNameTest()
        {
            IBuproMembershipProvider mp = new IBuproMembershipProvider();
            Assert.AreEqual(mp.ApplicationName, "ibupro");
            mp.ApplicationName = null;
        }

        /// <summary>
        ///A test for EnablePasswordReset
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void EnablePasswordResetTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            bool actual;
            actual = target.EnablePasswordReset;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for EnablePasswordRetrieval
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void EnablePasswordRetrievalTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            bool actual;
            actual = target.EnablePasswordRetrieval;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for MaxInvalidPasswordAttempts
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException))]
        public void MaxInvalidPasswordAttemptsTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            int actual;
            actual = target.MaxInvalidPasswordAttempts;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for MinRequiredNonAlphanumericCharacters
        ///</summary>
        [TestMethod()]
        public void MinRequiredNonAlphanumericCharactersTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            int? actual;
            actual = target.MinRequiredNonAlphanumericCharacters;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for MinRequiredPasswordLength
        ///</summary>
        [TestMethod()]
        public void MinRequiredPasswordLengthTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            int actual;
            actual = target.MinRequiredPasswordLength;
            Assert.AreEqual(0, actual);
        }

        /// <summary>
        ///A test for PasswordAttemptWindow
        ///</summary>
        [TestMethod()]
        public void PasswordAttemptWindowTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            int? actual;
            actual = target.PasswordAttemptWindow;
            Assert.IsNotNull(actual);

        }

        /// <summary>
        ///A test for PasswordFormat
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException),"Neni implementovano")]
        public void PasswordFormatTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            MembershipPasswordFormat actual;
            actual = target.PasswordFormat;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for PasswordStrengthRegularExpression
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException), "Neni implementovano")]
        public void PasswordStrengthRegularExpressionTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            string actual;
            actual = target.PasswordStrengthRegularExpression;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for RequiresQuestionAndAnswer
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException), "Neni implementovano")]
        public void RequiresQuestionAndAnswerTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            bool actual;
            actual = target.RequiresQuestionAndAnswer;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for RequiresUniqueEmail
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(NotImplementedException), "Neni implementovano")]
        public void RequiresUniqueEmailTest()
        {
            IBuproMembershipProvider target = new IBuproMembershipProvider(); 
            bool actual;
            actual = target.RequiresUniqueEmail;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
